package com.b1.taskmanager.listener;

import com.b1.taskmanager.api.TaskList;
import com.b1.taskmanager.start.gui.AddWindow;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import org.apache.log4j.Category;
import org.apache.log4j.Logger;
import javax.swing.JOptionPane;



public class ViewDates implements java.util.Observer
{
    private static final Logger log = Logger.getLogger(ViewDates.class);
    private TaskList taskList;
    private HashMap<Date, String> taskMap = new HashMap<Date, String>();
    private SimpleDateFormat sdf = new SimpleDateFormat("HH:mm dd-MM-yyyy");
    private boolean flag = true;
    
    public ViewDates()
    {
        log.info("Start Listener");
        update();
    }
    
    public void update() 
    {
        try 
        {
            taskList = new TaskList();
            //flag = true;
            for (int i = 0; i < taskList.getTaskList().size(); i++) 
            {
                taskMap.put(sdf.parse(taskList.listToString()[i][1]), taskList.listToString()[i][0]);
            }
            Set<Date> taskTimeSet = taskMap.keySet();
            Date[] taskTime = (Date[]) taskTimeSet.toArray(new Date[taskTimeSet.size()]);
            while (flag) 
            {
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.MILLISECOND, 0);
                cal.set(Calendar.SECOND, 0);
                Date nowTime = cal.getTime();
                for (int i = 0; i < taskTime.length; i++) 
                {
                    if (nowTime.compareTo(taskTime[i]) == 0) 
                    {
                        JOptionPane.showMessageDialog(null, taskList.showTask(Integer.parseInt(taskMap.get(taskTime[i]))), "Came Event", JOptionPane.PLAIN_MESSAGE);
                        taskTime[i] = sdf.parse("00:00 1-01-1970");
                        break;
                    }
                }
            }
        } 
        catch (ParseException ex) 
        {
            log.debug(ViewDates.class.getName(),ex);
            //Logger.getLogger(ViewDates.class.getName()).log(Level.SEVERE, null, ex);
        }
   }
    public void update(java.util.Observable o,Object arg) 
    {
        update();
    }

    public HashMap<Date, String> getTaskMap() 
    {
        return taskMap;
    }

    public void setTaskMap(HashMap<Date, String> taskMap) 
    {
        this.taskMap = taskMap;
    }
    
    
    
}
